package com.liuyjy.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.liuyjy.mybatisplus.handler.ModTableNameParser;
import com.liuyjy.mybatisplus.handler.ValTableNameParser;
import com.liuyjy.mybatisplus.mpbase.MySqlInjector;
import com.liuyjy.mybatisplus.util.TableNameUtil;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * @Author liuyjy
 * @Date 2022/1/25 11:08
 * @Description: 配置信息
 **/
@Configuration
@MapperScan(basePackages = "${common.setting.mybatisplus.mapperPackag}")
public class MybatisPlusConfig {

    public static String appName;
    /**
     * 分表规则 取模
     */
    @Value("${common.setting.mybatisplus.modTables:}")
    private String modTables;
    /**
     * 分表规则 取模
     */
    @Value("${common.setting.mybatisplus.tables:}")
    private String tables;

    @Value("${spring.application.name}")
    private String applicationName;

    @PostConstruct
    public void init() {
        appName = applicationName;
    }

    // 注册乐观锁和分页插件(新版：3.4.0)
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //这里为不同的表设置对应表名处理器
        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>();

        //取模
        if (StringUtils.isNotBlank(modTables)) {

            for (String s : modTables.split(",")) {
                String[] strings = s.split("&");
                if (strings.length != 2) {
                    throw new RuntimeException("分表配置错误，请按配置规则进行配置！");
                }
                Integer mod = Integer.valueOf(strings[1]);
                if (mod <= 0) {
                    throw new RuntimeException("分表配置错误，请按配置规则进行配置！");
                }
                map.put(strings[0], new ModTableNameParser());

                //初始化表名信息
                TableNameUtil.setMod(strings[0], mod);
            }
        }

        //手动设置下标
        if (StringUtils.isNotBlank(tables)) {
            for (String table : tables.split(",")) {
                map.put(table, new ValTableNameParser());
                //初始化表名信息
                TableNameUtil.setValTable(table);
            }
        }

        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        //分表处理
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

        //乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // DbType：数据库类型(根据类型获取应使用的分页方言)  分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public MySqlInjector sqlInjector() {
        return new MySqlInjector();
    }


    /*
     * 解决druid 日志报错：discard long time none received connection:xxx
     * */
    @PostConstruct
    public void setProperties() {
        System.setProperty("druid.mysql.usePingMethod", "false");
    }


}
